package io.renren.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.R;
import io.renren.entity.CCSPositionEntity;
import io.renren.entity.SysUserEntity;
import io.renren.form.CCSPositionForm;
import io.renren.service.CCSPositionService;
import io.renren.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 位置接口
 * 
 * @author SongR
 *
 */
@RestController
@RequestMapping("/api")
@Api(tags = "位置接口")
public class ApiPositionController {

	@Autowired
	CCSPositionService service;

	@Autowired
	SysUserService uService;
	/**
	 * 添加位置信息
	 * 
	 * @param entity
	 * @return
	 */
	@PostMapping("addPosition")
	@ApiOperation(value = "添加位置信息")
	public R addPosition(@ApiParam(name="position",value="位置信息",type="Object",required=true) CCSPositionForm position) {
		CCSPositionEntity entity = new CCSPositionEntity();
		entity.setPositionXaxis(position.getPositionXaxis());
		entity.setPositionYaxis(position.getPositionYaxis());
		entity.setUserId(position.getUserId());
		entity.setPositionCdate(new Date());
		SysUserEntity user =uService.selectById(position.getUserId());
		if(null!=user){
			service.insert(entity);
		}else{
			return R.error().put("msg", "该用户不存在！");
		}
		return R.ok();
	}

	/**
	 * 获取位置信息
	 * 
	 * @return
	 */
	@GetMapping("getPosition")
	@ApiOperation(value = "获取位置信息", response = CCSPositionEntity.class)
	@ApiImplicitParam(name="userId",value="用户编号",dataType="Long", paramType = "path",defaultValue="1",required=true)
	public R getPosition(@ApiIgnore @ApiParam(name="用户编号",value="请传入用户编号",required=true) Long userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", userId);
		List<CCSPositionEntity> positions = service.selectByMap(map);
		return R.ok().put("result", positions);
	}
}
